package com.echoesnet.echoesshortvideo.Activities;

import android.app.Activity;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.baidu.mapapi.search.core.PoiInfo;
import com.echoesnet.echoesshortvideo.Constant.EsvConstant;
import com.echoesnet.echoesshortvideo.R;
import com.echoesnet.echoesshortvideo.ViewModels.SelectLocationModel;
import com.echoesnet.echoesshortvideo.views.adapters.SearchLocationAdapter;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Copyright (C) 2017 在线回声（天津）科技发展有限公司
 * 在线回声完全享有此软件的著作权，违者必究
 *
 * @author an
 * @version 1.0
 * @modifier
 * @createDate 2018/4/25 0025
 * @description
 */
public class SelectLocationAct extends BaseActivity
{
    private static final String TAG = SelectLocationAct.class.getSimpleName();

    @BindView(R.id.rv_near)
    RecyclerView recyclerView;

    private List<PoiInfo> poiInfoList;
    private SearchLocationAdapter searchLocationAdapter;
    private SelectLocationModel selectLocationModel;
    private Activity mAct;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_location_act);
        ButterKnife.bind(this);
        mAct = this;
        poiInfoList = new ArrayList<>();
        selectLocationModel = ViewModelProviders.of(this).get(SelectLocationModel.class);

        searchLocationAdapter = new SearchLocationAdapter(mAct,poiInfoList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(searchLocationAdapter);
        searchLocationAdapter.setItemClickListener(new SearchLocationAdapter.ItemClickListener()
        {
            @Override
            public void itemClick(PoiInfo poiInfo)
            {
                Intent intent = new Intent();
                intent.putExtra("isShow",true);
                intent.putExtra("locationName",poiInfo.name);
                intent.putExtra("latitude",String.valueOf(poiInfo.location.latitude));
                intent.putExtra("longitude",String.valueOf(poiInfo.location.longitude));
                setResult(RESULT_OK,intent);
                finish();
            }

            @Override
            public void noShowLocationClick()
            {
                Intent intent = new Intent();
                intent.putExtra("isShow",false);
                setResult(RESULT_OK,intent);
                finish();
            }
        });
        selectLocationModel.searchLocation().observe(this,poiInfos -> {
            poiInfoList.clear();
            if (poiInfos != null)
            {
                poiInfoList.addAll(poiInfos);
            }
            searchLocationAdapter.notifyDataSetChanged();
        });
    }

    @OnClick({R.id.img_back,R.id.ll_search})
    public void onViewClick(View view){
        switch (view.getId())
        {
            case R.id.img_back:
                finish();
                break;
            case R.id.ll_search:
                Intent intent = new Intent(mAct,SearchLocationAct.class);
                startActivityForResult(intent,EsvConstant.OPEN_SEARCH_LOCATION);
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == EsvConstant.OPEN_SEARCH_LOCATION && resultCode == RESULT_OK)
        {
            Intent intent = new Intent();
            intent.putExtra("isShow",data.getBooleanExtra("isShow",false));
            intent.putExtra("locationName",data.getStringExtra("locationName"));
            intent.putExtra("latitude",data.getDoubleExtra("latitude",0));
            intent.putExtra("longitude",data.getDoubleExtra("longitude",0));
            Logger.t("select>").d(intent.toString());
            setResult(RESULT_OK,intent);
            finish();
        }
    }
}
